

const account = require("../../../js/modules/account.js");
const info = require("../../../js/modules/info.js");
import { forms,storage,util,api} from '../../../js/util';
import BasePage from '../../../js/common/base-page'
var app = getApp();
BasePage({
  pageKey: "password",
  validateLogin: false,
  /**
   * 页面的初始数据
   */
  data: {
    pageType: 1, //1:登录页面  2:注册页面  3:找回密码
    step: 0,
    step1: "display:block;",
    step2: "display:none;",
    step3: "display:none;",
    title1: "on",
    title2: "",
    title3: "",
    example: {
      classNames: 'ec-animate--fadeIn',
      enter: true,
      exit: true,
      in: true
    },
    forms: {
      Mobile: {
        value: "",
        validate: "isnull|mobile",
        error: "请输入正确的手机号"
      },
      PicVerifyCode: {
        value: "",
        validate: "isnull",
        error: "请输入正确的校验码"
      },
      ValidateCode: {
        value: "",
        validate: "isnull",
        error: "请输入正确的短信校验码"
      },
      Password: {
        value: "",
        validate: "isnull|pwd",
        error: "密码长度为6-20位"
      },
      RepPassword: {
        value: "",
        validate: "isnull|pwd",
        error: "请输入正确的确认密码"
      },
    },
    current: null,
    role_list: [{
        title: "零售商",
        role: "store"
      },
      {
        title: "供应商",
        role: "supplier"
      }
    ],
    notifyTime: 60,
    isSendSmsCode: false, //是否已发送短信校验码
    totleHeight: '603px',
    bg_pic: "",
    mainColor:'',//主题色
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that=this;

    var role = "store";
    if (options.role) {
      role = options.role;
    }
    for (var i in this.data.role_list) {
      var item = this.data.role_list[i];
      if (item.role == role) {
        this.setData({
          current: item
        })
        break;
      }
    }
    this.get_website_config();
    // //获得图片验证码组件
    // this.picVerifyCode = this.selectComponent("#picVerifyCode");
    // //设置回调
    // this.picVerifyCode.data.callback = info.getPicVerifyCode;
    // //调用一次图片验证码
    // this.picVerifyCode.bindPicVerifyCode();

    //获得发送短信组件
    this.smsVerifyCode = this.selectComponent("#smsVerifyCode");
    //设置回调
    this.smsVerifyCode.data.callback = this.bindSmsVerifyCode;

  },
  /**
   * 表单值绑定
   */
  bindFormValue: function(e) {
    e = e.detail;
    this.data.forms[e.target.id].value = e.detail.value;

  },
  /**
   * 校验手机号 
   * */
  bindValidateUserName: function() {
    var that = this;
    // 设置能否发送短信校验码为false
    that.smsVerifyCode.data.isSendSmsCode = false;
    that.data.mobileToken = "";
    that.data.mobileMsg = "";
    var mobile = that.data.forms.Mobile.value;
    //验证手机号
    if (!forms.validate(that.data.forms.Mobile)) return false;

    //组装参数
    var param = {
      mobile: mobile,
      ProprietorId: app.dns.proprietorId,
      ZoneId: app.dns.zoneId
    };

    account.password.validateUserName(that.data.current.role,param,
      function(res) {
        if (res.Success) {
          // that.data.mobileToken = res.Content.ForgetToken;
          that.setData({
            mobileToken:res.Content.ForgetToken
          })
          that.smsVerifyCode.data.isSendSmsCode = false;
        } else {
          that.smsVerifyCode.data.isSendSmsCode = true;
          that.setData({
            mobileMsg:res.Message
          })
          util.alert(res.Message);
          return;
        }
      });
  },
  /*
  发送短信验证码
   */
  bindSmsVerifyCode: function(callback) {
    var that = this;
    if (!this.step1_validate()) return;
    var mobile = this.data.mobileToken;
    var picvalidatecode = this.data.forms.PicVerifyCode.value;
    var codeToken = storage.get("pic_verify_code");
    var data = {
      mobile: mobile,
      forgetType: 0,
      verifyCode: picvalidatecode,
      codeToken: codeToken,
      Proprietor: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId
    };
    info.sms.getSmsVerifyCode(data, "account." + that.data.current.role+".forget.validatecode.get",function(content){
      if(callback&&typeof(callback)=="function"){
        callback(content);
      }
    });
  },
  /**
   * 验证手机
   */
  step1_validate: function() {
    var mobile = this.data.mobileToken;
    if (mobile == undefined || mobile.length == 0) {
      util.alert("请输入注册的手机号码");
      return false;
    }
    // var picvalidatecode = this.data.forms.PicVerifyCode.value;
    // if (picvalidatecode.length == 0) {
    //   util.alert("请先输入4位验证码");
    //   return false;
    // }
    // var codeToken = storage.get("pic_verify_code");
    // if (codeToken == undefined || codeToken.length == 0) {
    //   util.alert("图片验证码过期");
    //   return false;
    // }
    return true;
  },
  /**
   * 验证步骤一 
   * */
  step1: function() {
    var that = this;
    if (!this.step1_validate()) return;
    var mobile = this.data.mobileToken;
    var verifyCode = this.data.forms.ValidateCode.value;
    var param = {
      mobile: mobile,
      ForgetType: 0,
      verfiyCode: verifyCode
    };
    account.password.verify(this.data.current.role,param,
      function(res) {
        if (res.Success) {
          that.setData({
            step: 1,
            step1: "display:none;",
            step2: "display:block;",
            step3: "display:none;",
            title1: "on",
            title2: "on",
            title3: ""
          });
        } else {
          util.alert(res.Message);
          return;
        }
      });
  },
  /**
   * 重置密码 
   * */
  step2: function(param) {
    var that=this;
    var mobile = this.data.mobileToken;
    if (!forms.validate(this.data.forms.Password)) return false;
    if (!forms.validate(this.data.forms.RepPassword)) return false;
    if (!forms.compare(this.data.forms.Password, this.data.forms.RepPassword)) return;

    var param = {
      forgetToken: mobile,
      forgetType: 0,
      password: this.data.forms.Password.value
    };
    account.password.forget(this.data.current.role,param,
      function(res) {
        if (res.Success) {
          that.setData({
            step: 2,
            step1: "display:none;",
            step2: "display:none;",
            step3: "display:block;",
            title1: "on",
            title2: "on",
            title3: "on"
          });
        } else {
          util.alert(res.Content);
        }
      });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    wx.setNavigationBarTitle({
      title: api.title
    });
  },
  /**加载站点信息 */
  get_website_config() {
    var that = this;
    let { logo, bg_pic, imgDomain } = this.data;
    if (app.dns.proprietorId != app.dns.fkId) return;
    that.setData({
      logo: api.webSite.logo.length > 0 ? api.webSite.logo : logo,
      bg_pic: api.webSite.bgPic.length > 0 ? api.webSite.bgPic : bg_pic,
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      mainColor:api.titleColor||"#e56b14"
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  onExited() {
    this.setData({
      pageType: this.data.pageIndex
    });
  }
});